#define _CRT_SECURE_NO_WARNINGS  1


class Solution
{
public:
    int minOperations(vector<int>& nums, int x)
    {
        int left = 0;
        int right = 0;
        int sum = 0;
        int Sum = 0;
        int count = -1;

        for (int a : nums)
        {
            sum += a;
        }
        int target = sum - x;
        if (target < 0)
            return -1;
        while (right < nums.size())
        {
            Sum += nums[right];
            while (Sum > target)
            {
                Sum -= nums[left];
                left++;
            }
            if (Sum == target)
            {
                if (count < right - left + 1)
                    count = right - left + 1;
            }
            right++;
        }
        if (count == -1)
            return count;
        else return nums.size() - count;
    }
};